<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=s, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 150px;
            height: 150px;
            background-color: skyblue;
            position: absolute;
            left: 0;
            top: 0;
            /* z-index: ; */
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <script>
        var box = document.querySelectorAll(".box")

        function DragEle(ele) {
            this.target = ele;
            this.into();
        }
        DragEle.prototype.into = function () {
            this.dragStar()
            this.dragcolor()
        }
        DragEle.prototype.dragStar = function () {

            var _this = this//指向DragEle
            this.target.onmousedown = function (e) {
                var e = e || window.event;
                var posx = e.offsetX;
                var posY = e.offsetY;
                console.log(this);
                console.log(_this.target);
                _this.posx = posx;
                _this.posY = posY;
                console.log(_this);
                this.style.zIndex = 999
                _this.dragmove();
                _this.dragend();

            }
        }
        DragEle.prototype.dragmove = function () {
            document.onmousemove = (e) => {
                var e = e || window.event
                this.target.style.left = (e.pageX - this.posx) + "px";
                this.target.style.top = (e.pageY - this.posY) + "px";
            }
        }
        DragEle.prototype.dragend = function () {
            var _this = this
            document.onmouseup = function () {
                document.onmousemove = null;
                // ele.onmousedown = null;
                document.onmouseup = null;
                _this.target.style.zIndex = "auto";
            }
        }
        DragEle.prototype.dragcolor = function () {
            var str = "0123456789abcdef"
            var color = "#"
            for (var i = 0; i < 6; i++) {
                var index = Math.floor(Math.random() * str.length)
                color += str.charAt(index)
            }
            console.log(color);
            this.target.style.backgroundColor = color
        }
        for (var i = 0; i < box.length; i++) {
            var list = box[i]
            var a = new DragEle(list)
        }

        // 普通函数和构造函数的区别

        // box.hanshu()
    </script>
</body>

</html>